const app = getApp()
const { ActivityModel } = require('../../../utils/activity-model.js')
const activityModel = new ActivityModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: '',
    merchantId: '',
    cityCode: '',
    cityName: '',
    shopId: '',
    shopName: '',
    title: '',
    phoneNumber: '',
    isLogin: true,
    phoneShow: false,
    encryptedDataUser: '',
    ivUser: '',
    ruleShow: false,
    activityInfo: {},
    tabIdx: 0,
    tabs: [
      { label: '全国排行榜', value: 0 },
      { label: '战区排行榜', value: 1 },
      { label: '门店排行榜', value: 2 }
    ],
    myRankInfo: {
      phoneNumber: '',
      goldNum: 0
    },
    joinWarZone: true,
    goldRecordRankList: [],
    rankShowMore: false
  },

  goExchange() {
    wx.navigateTo({
      url: `/pages/activity/coinRank/exchange/index?shopId=${this.data.shopId}&shopName=${this.data.shopName}&activityId=${this.data.activityId}&merchantId=${this.data.merchantId}&cityCode=${this.data.cityCode}&cityName=${this.data.cityName}`
    })
  },
  lookRule() {
    this.setData({
      ruleShow: true
    })
  },
  closeRule() {
    this.setData({
      ruleShow: false
    })
  },
  getUserInfo({ detail }) {
    const { encryptedData, iv } = detail
    if (encryptedData) {
      this.setData({
        encryptedDataUser: encryptedData,
        ivUser: iv,
        phoneShow: true
      })
    }
  },
  tabChange(e) {
    const { index } = e.currentTarget.dataset
    if (index !== this.data.tabIdx) {
      this.setData({
        tabIdx: index
      })
      this.getCoinInfo()
    }
  },
  changeMore() {
    this.setData({
      rankShowMore: !this.data.rankShowMore
    })
  },
  getCoinInfo() {
    const type = this.data.tabs[this.data.tabIdx].value
    activityModel.goldRankView({
      type,
      shopId: this.data.shopId,
      activityId: this.data.activityId
    }).then(data => {
      if (type !== 1) {
        data.joinWarZone = true
      }
      this.setData({
        joinWarZone: !!data.joinWarZone,
        goldRecordRankList: data.goldRecordRankList || []
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
    activityModel.goldRankSelf({
      type,
      shopId: this.data.shopId,
      activityId: this.data.activityId,
      phoneNumber: this.data.phoneNumber
    }).then(data => {
      this.setData({
        myRankInfo: data || {}
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  refresh() {
    app.getInfo().then(({data}) => {
      this.setData({
        isLogin: data.login,
        phoneNumber: data.appletMobile
      })
      this.getCoinInfo()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      activityId: options.id,
      shopId: options.shopId,
      shopName: options.shopName,
      cityCode: options.cityCode,
      cityName: options.cityName
    })
  },
  onShow() {
    app.getInfo().then(({data}) => {
      this.setData({
        isLogin: data.login,
        phoneNumber: data.appletMobile,
        merchantId: data.merchantId
      })
      activityModel.goldDetail({
        activityId: this.data.activityId,
        merchantId: data.merchantId
      }).then(data => {
        this.setData({
          activityInfo: data.activityRule || {},
          title: data.activityName
        })
        wx.setNavigationBarTitle({
          title: data.activityName
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none'
        })
      })
      if (data.login) {
        this.getCoinInfo()
      }
    })
  },
  onShareAppMessage() {
    return {
      title: this.data.title,
      imageUrl: this.data.activityInfo.shareImagePath,
      path: `pages/activity/coinRank/index?shopId=${this.data.shopId}&id=${this.data.activityId}&shopName=${this.data.shopName}&cityCode=${this.data.cityCode}&cityName=${this.data.cityName}`
    }
  }
})
